package com.hotelManagerSystem.controller;


import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.User;
import com.hotelManagerSystem.service.IUserService;
import com.hotelManagerSystem.util.PageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqx
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService iUserService;

    //登录界面
    @RequestMapping("/loginUI")
    public String loginUI() {
        return "login";
    }

    //用户信息页面
    @RequestMapping("/userInfoUI")
    public String userInfoUI(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        String username = (String) currentUser.getSession().getAttribute("username");
        model.addAttribute("username", username);
        return "user/user-info";
    }
    //注册用户界面
    @RequestMapping("/registerUI")
    public String registerUI() {
        return "user/register";
    }

    //用户列表界面
    @RequestMapping("/listUI")
    public String listUI() { return "user/user-list"; }

    //添加用户界面
    @RequestMapping("/userAddUI")
    public String userAddUI() { return "user/user-add"; }

    //更新用户界面
    @RequestMapping("/updateUI")
    public String updateUI() {
        return "user/user-edit";
    }

    //查询所有的用户信息
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> queryAll(String username) {
        List<User> list = iUserService.list();
        Map<String, Object> map = new HashMap<>();
        map.put("userList", list);
        return map;
    }

    //更新用户的信息
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateUser(User user) {
        return iUserService.updateUser(user);
    }

    //删除一个用户
    @RequestMapping("/del/{id}")
    @ResponseBody
    public Map<String, Object> delUser(@PathVariable("id")Integer id) {
        return iUserService.delUser(id);
    }

    //登录功能，用不上
    @RequestMapping("/login")
    public Map<String, Object> login(String username, String password) {
        iUserService.login(username, password);
        return null;
    }

    //根据用户名或姓名模糊查询
    @RequestMapping(value = "/queryByName", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryByName(String name) {
        return iUserService.queryByName(name);
    }

    //根据姓名查询
    @RequestMapping(value = "/queryByUserName")
    @ResponseBody
    public Map<String, Object> queryByUserName(String username) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(username);
        map.put("user", iUserService.queryByUsername(username));
        return map;
    }

    //用户的注册
    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> register(User user) {
        return iUserService.register(user);
    }

    @RequestMapping("/customer/register")
    @ResponseBody
    public Map<String, Object> registerUser(String username,String password){
        Map<String, Object> map = new HashMap<>();
        int flag = iUserService.registerUser(username,password);
        if (flag == 1){
            map.put("code",200);
            map.put("message","注册成功");
        }else {
            map.put("code",500);
            map.put("message","注册失败");
        }
        return map;
    }

    @RequestMapping("/getUsername")
    @ResponseBody
    public Map<String, Object> getUsername() {
        Subject currentUser = SecurityUtils.getSubject();
        //获取当前用户username
        String username = (String) currentUser.getSession().getAttribute("username");
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        return map;
    }

    //I:客户的黑白名单功能


    //一:查询所有,跳转到black-white-user.html显示数据
    @RequestMapping("/black-white")
    public String black_white(Model model,Integer pageNum,Integer pageSize ){
        PageUtil.runStartPage(pageNum,pageSize);
        List<User> list = iUserService.queryUserList();
        PageInfo pageInfo = PageUtil.getPageInfo(list);
        //System.out.println("pageInfo:"+pageInfo);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-user";
    }
    //二:黑名单功能---根据用户的id把该用户添加进黑名单
    @RequestMapping("/addUserById")
    public String addUserById(String uidStr){
        int uid = Integer.parseInt(uidStr);
        int i = iUserService.addUserById(uid);
        return "redirect:/user/black-white";
    }
    //三:黑名单功能:根据用户的id把该该用户从黑名单中移除(即黑名单的删除)

    @RequestMapping("/deleteUserById")
    public String deleteUserById(String uidStr){
        int uid = Integer.parseInt(uidStr);
        int i = iUserService.deleteUserById(uid);
        return "redirect:/user/black-white";
    }
    //四:查询所有黑名单用户
    @RequestMapping("/blackUserList")
    public String blackUserList(Model model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<User> userList = iUserService.selectBlackUserList();
        PageInfo pageInfo = PageUtil.getPageInfo(userList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-user";
    }

    //五:查询所有白名单用户
    @RequestMapping("/whiteUserList")
    public String whiteUserList(Model model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<User> userList = iUserService.selectWhiteUserList();
        PageInfo pageInfo = PageUtil.getPageInfo(userList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-user";
    }
    //六:用户名具有唯一性,使用用户名进行查询用户,从来根据查询到的数据进行黑名单的增加和删除
    @RequestMapping("/selectUserByUsername")
    public String selectUserByUsername(@RequestParam("username")String username, Model  model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<User> userList = iUserService.selectUserByUsername(username);
        PageInfo pageInfo = PageUtil.getPageInfo(userList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-user";
    }


    /**
     * @author treasy
     * @description 用于注册校验用户名是否已经存在
     */
    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String, Object> checkUsername(String username){
        Map<String, Object> map = new HashMap<>();
        User user = iUserService.queryByUsername(username);
        if (user!=null){
            map.put("code","500");
            map.put("message","用户名已存在请从新尝试");
        }else {
            map.put("code","200");
            map.put("message","用户名可用");
        }
        return map;
    }




}

